package com.future.hedongjun.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.baselib.activity.BaseActivity;
import com.future.baselib.activity.BaseListActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.hedongjun.R;
import com.future.hedongjun.home.adapter.CategoryAdapter;
import com.future.hedongjun.home.adapter.ClassAdapter;
import com.future.hedongjun.home.adapter.TopicAdapter;
import com.future.hedongjun.home.bean.ClassDetail;
import com.future.hedongjun.home.bean.TopicBean;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ClassListActivity extends BaseListActivity {

    private String title;
    private int type;
    private String class_id;
    private String zhuti_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(title);
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return type == 2 ? new GridLayoutManager(this, 2) : super.getLayoutManager();
    }

    @Override
    protected BaseQuickAdapter getAdapter() {
        return new TopicAdapter(null);
    }

    @Override
    protected Type getTypeToken() {
        return new TypeToken<List<TopicBean>>() {
        }.getType();
    }

    @Override
    protected TreeMap<String, String> getRequestParams() {
        TreeMap<String, String> params = new TreeMap<>();
        if (TextUtils.isEmpty(zhuti_id)) {//分类
            params.put("apiCode", "_moreclasslist_001");
            params.put("id", class_id);
        } else {//专题
            params.put("apiCode", "_allmoreclasslist_001");
            params.put("id", zhuti_id);
        }
        return params;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        title = extras.getString("name");
        class_id = extras.getString("class_id");
        zhuti_id = extras.getString("zhuti_id");
        type = extras.getInt("type");
    }

    /**
     * @param title 标题
     * @param type  显示类型 单排1or双排2
     */
    public static void launch(Context context, String title, String class_id, String zhuti_id, int type) {
        Intent intent = new Intent(context, ClassListActivity.class);
        intent.putExtra("name", title);
        intent.putExtra("class_id", class_id);
        intent.putExtra("zhuti_id", zhuti_id);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ClassDetail item = ((ClassAdapter) adapter).getItem(position);
        if (item == null || TextUtils.isEmpty(item.getId())) {
            return;
        }
        Intent intent = new Intent(this, ClassDetailActivity.class);
        intent.putExtra("id",item.getId());
        startActivity(intent);
    }
}
